package com.example.studyplanning.controller;

import com.example.studyplanning.entity.Task;
import com.example.studyplanning.entity.User;
import com.example.studyplanning.services.TaskServices;
import com.example.studyplanning.util.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("task")
public class TaskController {
    @Autowired
    private TaskServices taskServices;

    //查询所有任务，返回列表
    @GetMapping("/list")
    private Result findAllTask(){
        Result result = new Result();
        result.setCode(200);
        result.setMessage("查询成功");
        result.setObject(taskServices.findAllTask());
        return result;
    }

    //插入任务
    @PostMapping("/add")
    public Result InsertTask(@RequestBody Task task){
        Result result = new Result();
        int i = taskServices.insertTask(task);
        if(i==1){
            result.setCode(200);
            result.setMessage("添加成功");
        }else{
            result.setCode(400);
            result.setMessage("添加失败");
        }
        return result;
    }

    //删除所选任务
    @GetMapping("/del")
    public Result DelTaskByID(@RequestParam("taskID") Integer taskID){
        Result result = new Result();
        int i = taskServices.DelTaskByID(taskID);
        if (i==1){
            result.setCode(200);
            result.setMessage("删除成功");
        }else {
            result.setCode(400);
            result.setMessage("删除失败");
        }
        return result;
    };

    //更新任务信息
    @PostMapping("/update")
    public Result UpdateTask(@RequestBody Task task){
        Result result = new Result();
        int i = taskServices.UpdateTask(task);
        if (i==1){
            result.setCode(200);
            result.setMessage("更新成功");
        }else {
            result.setCode(400);
            result.setMessage("更新失败");
        }
        return result;
    };

    //根据任务名模糊查询任务信息
    @GetMapping("/lists")
    public Result findTaskByName(@RequestParam(value = "taskName",defaultValue = "") String taskName){
        Result result = new Result();
        result.setCode(200);
        result.setMessage("查询成功");
        result.setObject(taskServices.findTaskByName(taskName));
        return result;
    };

    //计时任务提前完成
    @PostMapping("/updateLive")
    public Result UpdateTaskLive(@RequestParam Integer taskID){
        Result result = new Result();
        int i = taskServices.UpdateTaskLive(taskID);
        if (i==1){
            result.setCode(200);
            result.setMessage("更新成功");
        }else {
            result.setCode(400);
            result.setMessage("更新失败");
        }
        return result;
    };

    //将任务由已完成更新为未完成
    @PostMapping("/updatetasklive")
    public Result UpdateLive(@RequestParam Integer taskID){
        Result result = new Result();
        int i = taskServices.UpdateLive(taskID);
        if (i==1){
            result.setCode(200);
            result.setMessage("更新成功");
        }else {
            result.setCode(400);
            result.setMessage("更新失败");
        }
        return result;
    };

    //根据输入时间输出任务信息
    @GetMapping("/date")
    public Result SelectByDate(@RequestParam("date_time") String date_time, @RequestParam("userID") Integer userID){
        Result result = new Result();
        result.setCode(200);
        result.setMessage("查询成功");
        result.setObject(taskServices.SelectByDate(date_time,userID));
        return result;
    }

    //查询今日列表任务
    @GetMapping("/todaylist1")
    public Result findByDate(@RequestParam("date_time") String date_time,@RequestParam("userID") Integer userID){
        Result result = new Result();
        result.setCode(200);
        result.setMessage("查询成功");
        result.setObject(taskServices.findByDate(date_time,userID));
        return result;
    }

    //查询今日计时任务
    @GetMapping("/todaylist2")
    public Result findListByDate(@RequestParam("date_time") String date_time,@RequestParam("userID") Integer userID){
        Result result = new Result();
        result.setCode(200);
        result.setMessage("查询成功");
        result.setObject(taskServices.findListByDate(date_time,userID));
        return result;
    }

    //taskQuery多条件查询任务
    @GetMapping("/getlist/{num}")
    public Result getAllTask(@PathVariable("num") Integer num,
                                 @RequestParam(value = "date_time",defaultValue = "") String date_time,
                                 @RequestParam(value = "taskID",defaultValue = "") Integer taskID,
                                 @RequestParam(value = "taskName",defaultValue = "") String taskName,
                                 @RequestParam(value = "taskClass",defaultValue = "") String taskClass,
                                 @RequestParam(value = "taskType",defaultValue = "") Integer taskType,
                                 @RequestParam(value = "taskTime",defaultValue = "") Integer taskTime,
                                 @RequestParam(value = "userID",defaultValue = "") Integer userID,
                                 @RequestParam(value = "taskLive",defaultValue = "") Integer taskLive)
    {
        Result result = new Result();
        PageInfo<Task> pageInfo = null;
        if (date_time.equals("")){
            pageInfo = taskServices.SelectTheTask(num,taskID,taskName,taskClass,taskType,taskTime,userID,taskLive);
        }else {
            pageInfo = taskServices.SelectTask(num,date_time,taskID,taskName,taskClass,taskType,taskTime,userID,taskLive);
        }
        result.setCode(200);
        result.setMessage("查询成功");
        result.setObject(pageInfo);
        return result;
    }

    //辅助多条件查询


    //任务编辑，更新任务信息
    @PostMapping("/updatetask")
    public Result UpdateTheTask(@RequestBody Task task){
        Result result = new Result();
        int i = taskServices.UpdateTheTask(task);
        if (i==1){
            result.setCode(200);
            result.setMessage("更新成功");
        }else {
            result.setCode(400);
            result.setMessage("更新失败");
        }
        return result;
    };

    //查询今日专注总时长
    @GetMapping("/sumtime")
    public Result SelectSumTime(@RequestParam("date_time") String date_time,@RequestParam("userID") Integer userID){
        Result result = new Result();
        result.setCode(200);
        result.setMessage("查询成功");
        result.setObject(taskServices.SelectSumTime(date_time,userID));
        return result;
    }

    //查询今日待办任务数
    @GetMapping("/sumtodo")
    public Result SelectSumTodo(@RequestParam("date_time") String date_time,@RequestParam("userID") Integer userID){
        Result result = new Result();
        result.setCode(200);
        result.setMessage("查询成功");
        result.setObject(taskServices.SelectSumTodo(date_time,userID));
        return result;
    }

    //查看今日专注任务数
    @GetMapping("/sumclock")
    public Result SelectSumClock(@RequestParam("date_time") String date_time,@RequestParam("userID") Integer userID){
        Result result = new Result();
        result.setCode(200);
        result.setMessage("查询成功");
        result.setObject(taskServices.SelectSumClock(date_time,userID));
        return result;
    }

    //查看今日完成待办任务数
    @GetMapping("/finishedtodo")
    public Result SelectFinishedTodo(@RequestParam("date_time") String date_time,@RequestParam("userID") Integer userID){
        Result result = new Result();
        result.setCode(200);
        result.setMessage("查询成功");
        result.setObject(taskServices.SelectFinishedTodo(date_time,userID));
        return result;
    }

    //查看今日完成专注任务数
    @GetMapping("/finishedclock")
    public Result SelectFinishedClock(@RequestParam("date_time") String date_time,@RequestParam("userID") Integer userID){
        Result result = new Result();
        result.setCode(200);
        result.setMessage("查询成功");
        result.setObject(taskServices.SelectFinishedClock(date_time,userID));
        return result;
    }


    //echarts
    //日新建任务总数柱状图
    @GetMapping("/all")
    public List<Task>getAlltask(){
        List<Task>list=taskServices.findAlltask();
        return list;
    }

    //日新建待办任务数柱状图
    @GetMapping("/taskToDo/list")
    public List<Task>getAlltaskToDo(){
        List<Task>list=taskServices.findAlltaskToDo();
        return list;
    }

    //日新建计时任务数柱状图
    @GetMapping("/taskClock/list")
    public List<Task>getAlltaskClock() {
        List<Task> list = taskServices.countAlltaskClock();
        return list;
    }

    //计时任务创建人数柱状图
    @GetMapping("/clockUser/list")
    public List<Task>getAllclockUser() {
        List<Task> list = taskServices.countAllclockUser();
        return list;
    }

    //待办任务创建人数柱状图
    @GetMapping("/toDoUser/list")
    public List<Task>getAlltoDoUser(){
        List<Task>list=taskServices.countAlltoDoUser();
        return list;
    }
}
